Ext.namespace("Sdp.CompaniesView");

Sdp.CompaniesView = Ext.extend(Ext.app.Module, {
	id : 'companies-view',

	init : function(){
		this.launcher = {
			text: 'Company',
			iconCls:'bogus',
			handler : this.createWindow,
			scope: this
		}
	},

	getDataSource : function(company) {
	},

	makePaggingBar : function(ds) {
		var pagging = new Ext.PagingToolbar({
			pageSize: 10,
			store: ds,
			displayInfo: true,
			displayMsg: 'Displaying customers {0} - {1} of {2}',
			emptyMsg: 'No customers to display',
		});

		return pagging;
	},

	buidCompanyView : function(company) {
		var companyView = new Ext.Panel({
			region:'north',
			split:true,
			border: false,
			autoScroll:true,
			collapsible:true,
			title:'Company view',
			height: 180,
			tbar: [
				{
					text: 'Edit',
					tooltip: 'Edit this company',
					iconCls: 'icon-edit'
				}, '-',
				{
					text: 'Delete',
					tooltip: 'Delete this company',
					iconCls: 'icon-remove'
				}
			]
	    });
	    return companyView;
	},

	buildGrid : function(company) {
		var companyView = this.buidCompanyView(company);

		var contactsModule = this.app.getModule('contacts-grid');
		var contactsGrid = contactsModule.buildGrid(company, {
			title: 'Contacts'
		});

		var quotationsModule = this.app.getModule('quotations');
		var quotationsGrid = quotationsModule.buildGrid(company, {
			title: 'Quotations'
		});

	    var p = new Ext.Panel({
	    	layout: 'border',
	    	items: [
				{
					region:'center',
					border:false,
					layout:'border',
					margins:'0 0 0 0',
					items:[
						companyView
					,{
						autoScroll:true,
						id: 'idTabPanel',
						activeTab: 0,
						elements:'body',
						region:'center',
						id:'Details',
						xtype: 'tabpanel',
						title:'Preview Panel',
						items: [
							quotationsGrid,
							contactsGrid,
						]
					}]
				}
	    	]
	    });

		Ext.Ajax.request({
		   url: 'index.php?m=companies&a=do_view&suppressHeaders=1&company_id=' + company.company_id,
		   success: function(response) {
				this.renderCompanyView(company, companyView, response.responseText);
		   },
		   failure: function() {
		   		alert('somthing wrong...');
		   },
		   headers: {
		       'my-header': 'foo'
		   },
		   params: { foo: 'bar' },

		   scope: this
		});


		return p;
	},

	renderCompanyView : function(company, p, data) {
		p.body.dom.innerHTML = data;
		p.setTitle("[" + company.company_code + "] " + company.company_name);
		p.syncSize();
        //p.body.highlight('#c3daf9', {block:true});
	},

	buildGridAction : function(grid) {
		// example of getting a reference to another module's launcher object
        var tabWinLauncher = MyDesktop.getModule('tab-win').launcher;

		grid.on({
			rowdblclick : function(g, rowIndex, e) {
				var record = g.getSelectionModel().getSelected();
        		e.stopEvent();
				//var m = this.app.getModule('cost-allocation-version-win');
				//m.createWindow({code: record.get('Code'), description: record.get('Description')});
				tabWinLauncher.handler.call(this.scope || this);
        	},

        	scope: this
		});

		// example of how to open another module on rowselect
//		sm.on('rowselect',function(){
//			tabWinLauncher.handler.call(this.scope || this);
//		}, this);
	},

	createWindow : function(company){
		var desktop = this.app.getDesktop();
		var win = desktop.getWindow('companies-view-' + company.company_id);
		if(!win){
			var p = this.buildGrid(company);
            win = desktop.createWindow({
                id: 'companies-view-' + company.company_id,
                title:'Customer view',
                width:740,
                height:480,
                iconCls: 'companies-view',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
				layout: 'fit',
                items: p
            });

			win.setTitle("[" + company.company_code + "] " + company.company_name);
		}
		win.show();
	}
});
